package com.ruoyi.wms.controller;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import com.ruoyi.wms.service.MailboxMessageService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.idempotent.annotation.RepeatSubmit;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.wms.domain.vo.MailboxStatisticsVo;
import com.ruoyi.wms.domain.bo.MailboxStatisticsBo;
import com.ruoyi.wms.service.MailboxStatisticsService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

/**
 * 数据统计
 *
 * @author zcc
 * @date 2024-12-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/statistics")
public class MailboxStatisticsController extends BaseController {

    private final MailboxStatisticsService mailboxStatisticsService;

    private final MailboxMessageService mailboxMessageService;


    /**
     * 查询数据统计列表
     */
    @SaCheckPermission("wms:statistics:list")
    @GetMapping("/list")
    public TableDataInfo<MailboxStatisticsVo> list(MailboxStatisticsBo bo, PageQuery pageQuery) {
        MailboxStatisticsVo Toady = mailboxStatisticsService.ToadyData();
        MailboxStatisticsBo MailboxStatisticsBo = new MailboxStatisticsBo();
        //日新增数量
        int today_quantity= mailboxMessageService.today_quantity();
        //周新增数量
        int week_qunatity= mailboxMessageService.week_qunatity();
        //月新增数量
        int month_quantity= mailboxMessageService.month_quantity();
        //当前信件总数
        int total= mailboxMessageService.total();
        //已处理数量
        int processed_total= mailboxMessageService.processed_total();
        //待处理数量
        int pending_total= mailboxMessageService.pending_total();

        //当日处理数量
        int today_processed_total= mailboxMessageService.today_processed_total();
        double efficiency= 0.0;
        //当日处理效率
        if (today_quantity != 0) {
            double processingEfficiency = (double) today_processed_total / today_quantity;
            DecimalFormat decimalFormat = new DecimalFormat("#0.0");
            String formattedEfficiency = decimalFormat.format(processingEfficiency * 100);
            efficiency= Double.parseDouble(formattedEfficiency);
        }
        MailboxStatisticsBo.setTodayQuantity((long) today_quantity);
        MailboxStatisticsBo.setWeekQuantity((long) week_qunatity);
        MailboxStatisticsBo.setMonthQuantity((long) month_quantity);
        MailboxStatisticsBo.setTotal((long) total);
        MailboxStatisticsBo.setProcessedTotal((long) processed_total);
        MailboxStatisticsBo.setPendingTotal((long) pending_total);
        MailboxStatisticsBo.setEfficiency(BigDecimal.valueOf(efficiency));
        MailboxStatisticsBo.setTodayProcessedTotal((long) today_processed_total);
        if(Toady==null){
            mailboxStatisticsService.insertByBo(MailboxStatisticsBo);
        }else {
            MailboxStatisticsBo.setId(Toady.getId());
            mailboxStatisticsService.updateByBo(MailboxStatisticsBo);
        }
        return mailboxStatisticsService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询数据统计列表
     */
    @SaCheckPermission("wms:statistics:list")
    @GetMapping("/newest")
    public MailboxStatisticsVo newest() {
        return mailboxStatisticsService.Newest();
    }

    /**
     * 导出数据统计列表
     */
    @SaCheckPermission("wms:statistics:export")
    @Log(title = "数据统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MailboxStatisticsBo bo, HttpServletResponse response) {
        List<MailboxStatisticsVo> list = mailboxStatisticsService.queryList(bo);
        ExcelUtil.exportExcel(list, "数据统计", MailboxStatisticsVo.class, response);
    }

    /**
     * 获取数据统计详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wms:statistics:query")
    @GetMapping("/{id}")
    public R<MailboxStatisticsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(mailboxStatisticsService.queryById(id));
    }

    /**
     * 新增数据统计
     */
    @SaCheckPermission("wms:statistics:add")
    @Log(title = "数据统计", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MailboxStatisticsBo bo) {
        mailboxStatisticsService.insertByBo(bo);
        return R.ok();
    }

    /**
     * 修改数据统计
     */
    @SaCheckPermission("wms:statistics:edit")
    @Log(title = "数据统计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MailboxStatisticsBo bo) {
        mailboxStatisticsService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除数据统计
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wms:statistics:remove")
    @Log(title = "数据统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        mailboxStatisticsService.deleteByIds(List.of(ids));
        return R.ok();
    }
}
